package stanford.com.balldating;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import cn.bmob.v3.Bmob;
import cz.msebera.android.httpclient.Header;
import stanford.com.balldating.util.HttpClientUtils;

public class ZhuceActivity extends AppCompatActivity {
    EditText ediname,edimail;
    EditText edipassword;
    RadioGroup selectsex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bmob.initialize(this, "87e563b7c9641d8d10cb9990943a4009");
        setContentView(R.layout.activity_zhuce);
        ediname=(EditText)findViewById(R.id.ediname) ;
        edipassword=(EditText)findViewById(R.id.edipassword) ;
        selectsex=(RadioGroup)findViewById(R.id.selectsex);

    }


    public void quedingzhuce(View v){
        int id;
        id=selectsex.getCheckedRadioButtonId();
        RadioButton r;
        r=(RadioButton)findViewById(id);

        RequestParams params=new RequestParams();
        params.add("username",ediname.getText().toString());
        params.add("password",edipassword.getText().toString());
        HttpClientUtils.post("register", params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject json = new JSONObject(responseString);
                    boolean success=json.getBoolean("success");
                    String msg=json.getString("msg");
                    if(success){
                        Intent it = new Intent(ZhuceActivity.this, MainActivity.class);
                        startActivity(it);
                    }else{
                        Toast.makeText(ZhuceActivity.this, msg, Toast.LENGTH_LONG).show();

                    }
                }catch (JSONException exception){
                }
            }
        });
        /*User user = new User();
        user.setname(ediname.getText().toString());
        user.setPassword(edipassword.getText().toString());
        user.setsex(r.getText().toString());
        user.save(new SaveListener<String>() {

            @Override
            public void done(String objectId, BmobException e) {
                if(e==null){
                    Toast.makeText(ZhuceActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                }else{

                }
            }
        });
        Intent it = new Intent(this, MainActivity.class);
        startActivity(it);*/
    }

    public void quxiaozhuce(View v) {
        Intent it = new Intent(this, MainActivity.class);
        startActivity(it);
    }

}
